clear all
set more off, perm
global cd 	"/mnt/brownresearch/ECON_LaPorta/rlaporta/research/BGLS2023"
global Tables	$cd/PNL/Tables
global Data	$cd/PNL/DataForTables
* *******************************************
set varabbrev off
* *******************************************
use $Data/DataTables_PNL, clear
keep if error+r5<.
tsset id datem
* winsorize
foreach var of varlist LTG error error3 ge5 pd pe {
	quietly: bysort datem: egen max=pctile(`var'), p(99)
	quietly: bysort datem: egen min=pctile(`var'), p(1)  
	quietly: replace `var'=max if `var'>max & `var'<.
	quietly: replace `var'=min if `var'<min	
	drop max min 
}
* standardsize
foreach var of varlist LTG error error3 ge5 pd pe r3 r5  {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}

tsset permco datem
gen L1LTG=L12.LTG
gen d1LTG=LTG-L1LTG
* *******************************************
* Table 6 
* *******************************************
preserve
keep if pd+d1LTG<.

* create variable that identifies "bubble" years
generate sample=1
replace  sample=0 if year(dofm(datem))>=1998 & year(dofm(datem))<=2002
replace  sample=0 if year(dofm(datem))>=2007 & year(dofm(datem))<=2009
* ***************
eststo clear
eststo: quietly: xi: xtivreg2 	error	d1LTG	L1LTG		i.datem			, fe robust dkraay(60) partial(i.datem)
eststo: quietly: xi: xtivreg2 	r5 	d1LTG	L1LTG		i.datem			, fe robust dkraay(60) partial(i.datem)
eststo: quietly: xi: xtivreg2 	r5 	(error=d1LTG L1LTG)	i.datem			, fe robust dkraay(60) partial(i.datem)
		 xi: weakiv xtivreg2 r5 (error=d1LTG L1LTG)	i.datem			, fe robust dkraay(60) partial(i.datem)
	quietly: xi: xtreg	error 	d1LTG L1LTG 		i.datem			, fe robust  
	predict fit if e(sample)
eststo: quietly: xi: xtivreg2 	r5 	fit  pd 		i.datem if pd>0		, fe robust dkraay(60) partial(i.datem)
eststo: quietly: xi: xtivreg2 	r5 	fit  pe 		i.datem			, fe robust dkraay(60) partial(i.datem)
eststo: quietly: xi: xtivreg2	r5 	(error=d1LTG L1LTG)	i.datem if sample==1	, fe robust dkraay(60) partial(i.datem)
esttab using $Tables/Table6.csv , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a rkf) title("Table 6") replace
* *******************************************
* Appendix D1
* *******************************************
restore
preserve
eststo clear
eststo: quietly: xi: xtivreg2 r5	d1LTG L1LTG ge5   i.datem, dkraay(60) fe robust partial(i.datem)
esttab using $Tables/AppendixD1_col2.csv , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a rkf) title("Appendix D1 -- Col [2]") replace
* *******************************************
* Appendix B10
* *******************************************
restore
keep if d1LTG+r5+ge5<.
eststo clear
eststo: quietly: xi: xtivreg2 	d1LTG	ge5 i.datem				, fe robust dkraay(12) partial(i.datem)
	quietly: xi: areg	d1LTG	ge5 i.datem				, absorb(permco)
	predict d1LTGhat if e(sample)

eststo: quietly: xi: xtivreg2	error	d1LTGhat L1LTG i.datem			, fe robust dkraay(60) partial(i.datem)
eststo: quietly: xi: xtivreg2 	r5	(error=d1LTGhat L1LTG) i.datem		, fe robust dkraay(60) partial(i.datem)
esttab using $Tables/AppendixB10.csv , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a rkf) title("Appendix B10") replace
* *******************************************
* Appendix B9
* *******************************************
use $Data/DataTables_PNL, clear
tsset id datem

keep if LTG+error3+r3<.
* winsorize
foreach var of varlist LTG error3 pd   {
	quietly: bysort datem: egen max=pctile(`var'), p(99)
	quietly: bysort datem: egen min=pctile(`var'), p(1)  
	quietly: replace `var'=max if `var'>max & `var'<.
	quietly: replace `var'=min if `var'<min	
	drop max min 
}
* standardsize
foreach var of varlist LTG error3 pd r3 r5   {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}

tsset permco datem
gen L1LTG=L12.LTG
gen d1LTG=LTG-L1LTG
* **********************
keep if pd+d1LTG<.
quietly: xi: xtreg 	error3 d1LTG L1LTG 			i.datem, fe robust  
predict fit if e(sample)

eststo clear
eststo: quietly: xi: xtivreg2 	error3	d1LTG	L1LTG			i.datem		, dkraay(36) fe robust partial(i.datem)
eststo: quietly: xi: xtivreg2 	r3 	d1LTG	L1LTG			i.datem		, dkraay(36) fe robust partial(i.datem)
eststo:	quietly: xi: xtivreg2 	r3 	(error3=d1LTG L1LTG)		i.datem		, dkraay(36) fe robust partial(i.datem)
eststo: quietly: xi: xtivreg2 	r3 	fit  pd 			i.datem	if pd>0	, dkraay(36) fe robust partial(i.datem)
esttab using $Tables/AppendixB9.csv , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a rkf) title("Appendix B9") replace
